package com.estar.assistsurvy.activity;

import java.io.Serializable;
import java.util.Calendar;
import java.util.List;

import com.estar.assistsurvy.R;
import com.estar.assistsurvy.entity.RequestTask;
import com.estar.assistsurvy.entity.ResponseOrder;
import com.estar.assistsurvy.entity.grabList;
import com.estar.assistsurvy.thread.TaskSupervisionThread;
import com.estar.assistsurvy.utils.ExitUtils;
import com.estar.assistsurvy.utils.MightypleUtil;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SearchTimeActivity extends BaseActivity implements OnClickListener{

	private TextView sTime,eTime;
	private RelativeLayout cancel;
	private Button search;
	private InputMethodManager imm;
	private Calendar calendar;
	private SharedPreferences prefer;
	private String deviceUUID,uUID;
	private String caseNumber="",targetCar="",taskState="",startTime="",endTime="";
	private Intent intent;
	private Handler handler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case -1:
				showShortToast((String) msg.obj);
				break;
				
			case 0:
				ResponseOrder task=new ResponseOrder();
				task=(ResponseOrder) msg.obj;
				if(task.getCode().equals("1")){
					List<grabList> grabList=task.getGrabList();
					if(grabList==null||grabList.size()==0){
						showShortToast("您的搜索结果为空!");
					}else{
						intent.putExtra("grabList", (Serializable) grabList);
						setResult(0, intent);
						finish();
					}
					
				}else if(task.getCode().equals("0")){
					showShortToast(task.getMessages());
				}else if(task.getCode().equals("2")){
					ExitUtils.Unlogin(SearchTimeActivity.this, task.getMessages());
				}
				break;

			default:
				break;
			}
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_searchtime);
		initView();
	}
	
	private void initView(){
		intent=getIntent();
		sTime=find(R.id.sTime);
		sTime.setOnClickListener(this);
		eTime=find(R.id.eTime);
		eTime.setOnClickListener(this);
		cancel=find(R.id.cancel);
		cancel.setOnClickListener(this);
		search=find(R.id.search);
		search.setOnClickListener(this);
		imm=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		calendar=Calendar.getInstance();
		prefer=getSharedPreferences("user", MODE_PRIVATE);
		uUID=prefer.getString("uuid", "");
		deviceUUID=MightypleUtil.getDeviceId(SearchTimeActivity.this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		imm.hideSoftInputFromWindow(v.getWindowToken(), 0); //强制隐藏键盘  
		switch (v.getId()) {
		case R.id.sTime:
			new DatePickerDialog(SearchTimeActivity.this, AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
				
				@Override
				public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
					// TODO Auto-generated method stub
					// 更新EditText控件日期 小于10加0
					sTime.setText(new StringBuilder()
                            .append(year)
                            .append("-")
                            .append((month + 1) < 10 ? "0"
                                    + (month + 1) : (month + 1))
                            .append("-")
                            .append((dayOfMonth < 10) ? "0" + dayOfMonth : dayOfMonth));
					eTime.requestFocus();
				}
			}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
			break;
			
		case R.id.eTime:
			new DatePickerDialog(SearchTimeActivity.this, AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
				
				@Override
				public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
					// TODO Auto-generated method stub
					// 更新EditText控件日期 小于10加0
					eTime.setText(new StringBuilder()
                            .append(year)
                            .append("-")
                            .append((month + 1) < 10 ? "0"
                                    + (month + 1) : (month + 1))
                            .append("-")
                            .append((dayOfMonth < 10) ? "0" + dayOfMonth : dayOfMonth));
					search.requestFocus();
				}
			}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
			
			break;
			
		case R.id.cancel:
			finish();
			break;
			
		case R.id.search:
			startTime=sTime.getText().toString().trim();
			endTime=eTime.getText().toString().trim();
			if(!startTime.equals("") && !endTime.equals("")){
				RequestTask requestTask=new RequestTask(deviceUUID, uUID, caseNumber, targetCar, taskState, startTime, endTime);
				new TaskSupervisionThread(SearchTimeActivity.this, handler, requestTask).start();
//				intent.putExtra("sTime", startTime);
//				intent.putExtra("eTime", endTime);
//				setResult(3, intent);
//				finish();
			}else{
				showShortToast("查询时间不能为空!");
			}
			break;

		default:
			break;
		}
	}
	
}
